                            # 6.1 一个简单的字典
alien_0 = { 'color' : 'green', 'points': 5 }
print( alien_0['color'] )
print( alien_0['points'] )
                            # 6.2 使用字典
                                # 字典是一系列 键-值
# 6.2.1 访问字典中的值
new_points = alien_0[ 'points' ]
print( "You just entered " + str( new_points ) + " points!" )
# 6.2.2 添加键-值对
alien_0['x_position'] = 0
alien_0['y_position'] = 0 # 新增二维位置
print(alien_0)
# 6.2.3 先创建一个空字典
alien_1 = {}    # 使用字典来存储用户提供的数据或在编写能自动生成大量键-值对的
                # 代码时，通常都需要先定义一个空字典
alien_1['color'] = 'red'
alien_1['points'] = 15
print(alien_1)
# 6.2.4 修改字典中的值
print("The alien is " + alien_0['color'] + '.')
alien_0['color'] = 'yellow'
print("The alien is now " + alien_0['color'] + '.')
    # 对一个能够以不同速度移动的外星人的位置进行跟踪
alien_0 = {'x_position' : 0, 'y_position' : 25, 'speed' : 'fast'}
print( "Orignal x-position: " + str(alien_0['x_position']) )
# 向右移动外星人
# 局外星人当前速度决定将其移动多远
if alien_0['speed'] == 'slow' :
    x_increment = 1
elif alien_0['speed'] == 'medium' :
    x_increment = 2
else :
    # 这个外星人的速度一定很快
    x_increment = 3
# 新位置等于老位置加上增量
alien_0['x_position'] = alien_0['x_position'] + x_increment
print( "New x-position: " + str( alien_0['x_position'] ) )
# 6.2.5 删除键-值对
del alien_1['color'] # 永远消失
# 6.2.6 由类似对象组成的字典
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python'
    }
print( "Jen's favorite language is " +
       favorite_languages['jen'].title()
       + "." )
# 6.1 遍历字典
# 6.3.1 遍历所有的键-值对
user_0 = {
    'username': 'efermi',
    'first': 'enrico',
    'last': 'fermi',
}
for key, val in user_0.items(): # 方法item()返回一个键-值对列表
    print( "\nKey: " + key )
    print( "Value: " + val )
for name, language in favorite_languages.items():
    print( "\nName: " + name.title() )
    print( "Favorite Language: " + language.title() )
# 6.3.2 遍历字典中的所有键
print( "\n" )
for name in favorite_languages.keys(): # 方法keys()返回字典中所有键
    print( "Name: " + name.title() )
print("\n")
friends = [ 'phil', 'sarah' ]
for name in favorite_languages.keys():
    print( name.title() )
    if name in friends:
        print( "Hi " + name.title() +
               ", I see your favorite language is " +
               favorite_languages[name].title() + "!" )
    if 'erin' not in favorite_languages.keys():
        print( "Erin, please take your poll!\n" )
# 6.3.3 按顺序遍历字典中所有键
for name in sorted( favorite_languages.keys() ): # 遍历前先对列表进行暂时排序
    print(name.title() + ", thank you for taking the poll!\n")
# 6.3.4 遍历字典中的所有值
    # 方法values()返回一个值列表，而不包含任何键
print( "The following languages have been mentioned:" )
for language in set( favorite_languages.values() ):
# 调用set()函数[集合]，找找出列表中独一无二的元素
    print( language.title() )
                            # 6.4 嵌套
# 6.4.1 字典列表
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
    print(alien)
print("\n")
# 创建一个存储外星人的空列表
aliens = []
# 创建30个绿色外星人
for alien_number in range(30):
    new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
    aliens.append(new_alien)

for alien in aliens[:3]:
    if alien['color'] == 'green':
        alien['color'] = 'yellow'
        alien['speed'] = 'medium'
        alien['points'] = 10
    elif alien['color'] == 'yellow':
        alien['color'] = 'red'
        alien['speed'] = 'fast'
        alien['points'] = 15
# 显示前五个外星人
for alien in aliens[:5]:
    print(alien)
print("...")
# 显示创建了多少个外星人
print( "Total number of aliens: " + str( len(aliens) ) )
# 在字典中存储列表
    # 在一个键中关联多个值
# 存储所点比萨的信息
pizza = {
    'crust': 'thick',
    'toppings': ['mushrooms', 'extra cheese'],
}
# 概述所点的比萨
print( "You odered a " + pizza['crust'] + "-crust pizza " +
       "with the following toppings" )
for topping in pizza['toppings']:
    print("\t" + topping)

favorite_languages = {
    'jen': ['python', 'ruby'],
    'sarah': ['c'],
    'edward': ['ruby', 'go'],
    'phil': ['python', 'haskell'],
    }
for name, languages in favorite_languages.items():
    print( "\n" + name.title() + "'s favorite languages are:" )
    if len( languages ) > 1:
        for language in languages:
            print( "\t" + language.title() )
    else:
        print( languages[0].title() )
# 6.4.3 在字典中存储字典
users = {
    'aeinstein':{
        'first': 'albert',
        'last': 'einstein',
        'location': 'princeton',
    },
    'mcurie':{
        'first': 'marie',
        'last': 'curie',
        'location': 'paris',
    },
}
for user_name, user_info in users.items():
    print("\nUserName: " + user_name)
    full_name = user_info['first'] + " " + user_info['last']
    location = user_info['location']

    print("\tFull name: " + full_name.title())
    print("\tLocation: " + location.title())